<template>
  <div class="resourceAdmin" v-loading="loading">
    <div class="title">菜单管理</div>
    <div class="content">
      <el-tree
        :data="resource"
        node-key="id"
        :props="defaultProps"
        @node-drop="handleDrop"
        @node-click="nodeClick"
        draggable
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span>
            <!-- <el-button
              type="text"
              size="mini"
              @click="() => append(data)">
              新增一项
            </el-button> -->
            <el-button type="text" size="mini" @click="() => remove(data)">
              删除
            </el-button>
          </span>
        </span>
      </el-tree>
    </div>
    <div
      class="infoContent"
      :class="{ show: Object.keys(isClickNode || {}).length > 0 }"
    >
      <p>修改面板</p>
      <el-form :model="form" :rules="formRules">
        <el-form-item label="节点名" prop="name">
          <el-input v-model="form.name" placeholder="节点名"></el-input>
        </el-form-item>
        <el-form-item label="路径(不要改这里)" prop="url">
          <el-input
            v-model="form.url"
            placeholder="路径"
            @keyup.enter.native="updateOne"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="greenBtn" size="small" @click="updateOne"
            >修改</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="floatBtn">
      <el-button class="greenBtn" size="small" @click="append"
        >新增一项</el-button
      >
      <el-button class="greenBtn" size="small" @click="doSave">保存</el-button>
    </div>
  </div>
</template>
<script>
import posts from "@/plugins/request";
export default {
  name: "resourceAdmin",
  data() {
    return {
      resource: [],
      resourceFlater: [],
      isClickNode: null,
      loading: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      form: {},
      formRules: {
        name: [{ required: true, message: "不能为空！", trigger: "blur" }],
        url: [{ required: true, message: "不能为空！", trigger: "blur" }],
      },
      maxId: 0,
    };
  },
  methods: {
    async getList() {
      this.loading = true;
      let result = await posts.fetchResourceList();
      this.resource = result.data.children;
      console.log('this.resource',this.resource)
      this.maxId = Number(result.maxId);
      this.resourceFlater = this.flat(result.children); //拉平树状结构
      this.loading = false;
    },
    handleDrop(draggingNode, dropNode) {
      let resource = this.resource;
      this.getName(resource, draggingNode.label).parentId =
        this.getName(resource, dropNode.label).parentId || null;
    },
    nodeClick(value) {
      this.isClickNode = value;
      this.form = value;
    },
    flat(arr) {
      return arr
        ? [].concat(
            ...arr.map((item) => [].concat(item, ...this.flat(item.children)))
          )
        : [];
    },
    getName(item, name) {
      let value = null;
      let ret = recurision(item, name);
      return ret;
      function recurision(item, name) {
        if (!item) return null;
        for (var i = 0; i < item.length; i++) {
          if (item[i].name == name) {
            value = item[i];
            break;
          }
          if (item[i].children) {
            recurision(item[i].children, name);
          }
        }
        return value;
      }
    },
    async doSave() {
      this.loading = true;
      let result = await posts.updateResourceList(this.resource);
      if (result.message !== "success") {
        this.$notify({
          title: "提示",
          message: "修改失败，稍后重试！",
          type: "warning",
        });
        return;
      }
      this.$notify({
        title: "提示",
        message: "修改成功！左侧导航重新登录即可更新！",
        type: "success",
      });
      this.getList();
      this.loading = false;
    },
    async updateOne() {
      this.loading = true;
      let result = await posts.putResource(this.form);
      if (result.message !== "success") {
        this.$notify({
          title: "提示",
          message: result.info,
          type: "warning",
        });
        return;
      }
      this.$notify({
        title: "提示",
        message: "修改成功！",
        type: "success",
        duration: 1000,
      });
      this.loading = false;
    },
    async append() {
      const newChild = { id: this.maxId++, name: "testtest", children: [] };
      // if (!data.children) {
      //   this.$set(data, 'children', []);
      // }addResource
      this.resource.push(newChild);
      let result = await posts.addResource({
        id: newChild.id,
        name: newChild.name,
        url: newChild.name,
        orderNum: this.resource.length,
      });
      if (result.message !== "success") {
        this.$notify({
          title: "提示",
          message: result.info,
          type: "warning",
        });
        return;
      }
      this.$notify({
        title: "提示",
        message: "修改成功！",
        type: "success",
        duration: 1000,
      });
      this.getList();
    },
    async remove(data) {
      this.$confirm(`确定删除${data.name}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let result = await posts.deleteResource(data.id);
        if (result.message !== "success") {
          this.$notify({
            title: "提示",
            message: result.info,
            type: "warning",
          });
          return;
        }
        this.$notify({
          title: "提示",
          message: "删除成功！",
          type: "success",
          duration: 1000,
        });
        this.isClickNode = {};
        this.getList();
      });
    },
  },
  async created() {
    this.getList();
  },
};
</script>
<style scoped>
.resourceAdmin {
  position: relative;
  min-height: 100%;
  background-color: #fff;
  padding: 0 20px;
  overflow-x: hidden;
}
.resourceAdmin .title {
  position: relative;
  left: 15px;
  line-height: 70px;
  font-size: 16px;
}
.resourceAdmin .title::before {
  content: "";
  position: absolute;
  left: -15px;
  width: 4px;
  height: 20px;
  top: 25px;
  background-color: #2399ff;
}
.content {
  width: 50%;
  min-height: 332px;
  border: 1px solid #e6e6e6;
  padding: 15px;
}
.infoContent {
  position: absolute;
  top: 70px;
  left: 100%;
  width: 35%;
  padding: 20px;
  border: 1px solid #e6e6e6;
  opacity: 0;
  transition: 0.3s ease;
}
.infoContent p {
  line-height: 40px;
  border-bottom: 1px solid #e6e6e6;
  margin-bottom: 15px;
}
.infoContent.show {
  left: calc(50% + 20px + 30px);
  opacity: 1;
}
.floatBtn {
  line-height: 60px;
  text-align: center;
}
.custom-tree-node {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>